// ignore_for_file: depend_on_referenced_packages

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:retrofit/http.dart';
import 'package:dio/dio.dart';

import '../json/LocalDateTimeConverter.dart';
import '../model/IntIdAndName.dart';
import '../utils/Page.dart';
import '../utils/local_datetime.dart';
import 'Initial.dart';

part 'GoodsApi.g.dart';
part 'GoodsApi.freezed.dart';

@RestApi()
abstract class GoodsApi {

  factory GoodsApi(Dio dio, {String baseUrl}) = _GoodsApi;

  @GET("/goods/barcodes/{barcode}/exists")
  Future<bool> exists(@Path("barcode") String barcode);

  @GET("/goods?projection=basic")
  Future<Page<GoodsBasic>> queryGoodsBasic(@Queries() Map<String,dynamic> queries);

  @GET("goods/{id}")
  Future<GoodsDetail> getGoodsDetail(@Path("id") int id);

  @POST("goods")
  Future<void> createGoods(@Body() AddGoodsRequest request);

  @PUT("goods/{id}")
  Future<void> editGoods(@Path() int id,@Body() AddGoodsRequest request);

}

@freezed
class AddGoodsRequest with _$AddGoodsRequest{

  const factory AddGoodsRequest({
    required String name,
    required int amount,
    required double price,
    required String barcode,
    int? categoryId,
    int? brandId,
    String? boxSpecification,
    int? costPrice,
    @Default([])
    List<EditGoodsUnitRequest> units,
    String? expressionPeriod,
    String? recordFilingNumber,
    String? registrationCertificateNumber,
    String? note,
    String? executiveStandard,
  }) = _AddGoodsRequest;

  const AddGoodsRequest._();

  factory AddGoodsRequest.fromJson(Map<String, dynamic> json) => _$AddGoodsRequestFromJson(json);

}

@freezed
class EditGoodsUnitRequest with _$EditGoodsUnitRequest{

  const factory EditGoodsUnitRequest({
    required String name,
    required int basicMultiple,
    int? id,
    required bool updatable,
  }) = _EditGoodsUnitRequest;

  const EditGoodsUnitRequest._();

  factory EditGoodsUnitRequest.fromJson(Map<String, dynamic> json) => _$EditGoodsUnitRequestFromJson(json);

}



@freezed
class GoodsUnitResponse with _$GoodsUnitResponse{

  const factory GoodsUnitResponse({
    required int id,
    required String name,
    required int basicMultiple,
    required bool updatable,
  }) = _GoodsUnitResponse;

  const GoodsUnitResponse._();

  factory GoodsUnitResponse.fromJson(Map<String, dynamic> json) => _$GoodsUnitResponseFromJson(json);

}

@freezed
class GoodsDetail with _$GoodsDetail{

  const factory GoodsDetail({
    required int id,
    @LocalDateTimeConverter() required LocalDateTime createdDateTime,
    required String name,
    required String barcode,
    required double price,
    required int amount,
    IntIdAndName? brand,
    IntIdAndName? category ,
    IntIdAndName? factory,
    String? unit ,
    String? boxSpecification,
    required List<GoodsUnitResponse> units,
    String? expressionPeriod,
    String? recordFilingNumber,
    String? registrationCertificateNumber,
    String? note,
    String? executiveStandard,
  }) = _GoodsDetail;

  const GoodsDetail._();

  factory GoodsDetail.fromJson(Map<String, dynamic> json) => _$GoodsDetailFromJson(json);

}


@freezed
class GoodsBasic with _$GoodsBasic{

  const factory GoodsBasic({
    required int id,
    required String name,
    required String barcode,
    required double price,
    required int amount,
  }) = _GoodsBasic;

  const GoodsBasic._();

  factory GoodsBasic.fromJson(Map<String, dynamic> json) => _$GoodsBasicFromJson(json);

}

GoodsApi goodsApi = GoodsApi(dio, baseUrl: baseUrl);
